/*
 * coveringPackingmMiscellaneous.cpp
 *
 *  Created on: May 14, 2013
 *      Author: sjelic
 */
#include "coveringPackingMiscellaneous.h"
namespace Miscellaneous{
	template <typename T>
	T** AllocateDynamicArray( int nRows, int nCols)
	{
		  T **dynamicArray;

		  dynamicArray = new T*[nRows];
		  for( int i = 0 ; i < nRows ; i++ )
		  dynamicArray[i] = new T [nCols]();

		  return dynamicArray;
	}

	template <typename T>
	void FreeDynamicArray(T** dArray)
	{
		  delete [] *dArray;
		  delete [] dArray;
	}
	template<class T>
	void printContainer(T  *ptr, index size){
	     index i;
	     std::cout << "[ ";
	     for(i=0;i<size;i++) std::cout << ptr[i] << " , " ;
	     std::cout << "\b\b]";
		 std::cout << std::endl;
	}
	template<class T>
	void printMatrix(T *ptr, index rows,index cols)
	{
	   index i;
	   for(i=0;i<rows*cols;i++){
	     if(i%cols==0) std::cout << "\b\n";
	     std::cout << ptr[i] << " " ;
	   }
	   std::cout << std::endl;

	}

	void experimentUsage()
	{
		std::cout<<"------------------Numerical Experiments-----------------------"<<std::endl;
		std::cout<<std::endl;
		std::cout<<std::endl;
		std::cout<<"      Methods: "<<std::endl;
		std::cout<<"             (1) GUROBI Optimizer                             "<<std::endl;
		std::cout<<"             (2) Parallel Derandomized Young solver   					  "<<std::endl;
		std::cout<<"             (3) Parallel Randomized Young solver   					  "<<std::endl;
		std::cout<<"             (4) Sequential Randomized Young solver   					  "<<std::endl;
		std::cout<<"             (5) Convert Rail to Young Sparse Format   					  "<<std::endl;
		std::cout<<std::endl;
		std::cout<<std::endl;
		std::cout<<"      Modes: "<<std::endl;
		std::cout<<"             (1) Numerical experiment on single instance.     "<<std::endl;
		std::cout<<"                 You will be asked for path to the instance   "<<std::endl;
		std::cout<<"                 data file and precision eps.                 "<<std::endl;
		std::cout<<"             (2) Numerical experiment on multiple instances.  "<<std::endl;
		std::cout<<"                 You will be asked for path to the directory  "<<std::endl;
		std::cout<<"                 that contains instance data files.           "<<std::endl;
		std::cout<<"                 You will be also asked to give number of     "<<std::endl;
		std::cout<<"                 epsilons and to type each in each of these   "<<std::endl;
		std::cout<<"                 epsilons in row.                             "<<std::endl;
		std::cout<<std::endl;
		std::cout<<std::endl;
		return;
	}

	//classes

	Timer::Timer(){ reset(); }
	void Timer::reset(){ _usecs = _usecs_at_mark = 0; _running = false; }
	void Timer::mark(){ stop(); _usecs_at_mark = _usecs; cont(); }
	void Timer::start()
	{
		_usecs = _usecs_at_mark = 0;
		gettimeofday(&_tstart, &_tz);
		_running = true;
	}
	void Timer::cont()
	{
		  if (_running == false)
		  {
		gettimeofday(&_tstart, &_tz);
		_running = true;
		  }
	}
	void Timer::stop()
	{
		gettimeofday(&_tend, &_tz);
		_running = false;
		_usecs += (off_t)(1000000) * (off_t)(_tend.tv_sec - _tstart.tv_sec) + (off_t)(_tend.tv_usec - _tstart.tv_usec);
		//return _usecs;
	}
	off_t Timer::value() const { return _usecs; }
	off_t Timer::usecs() const { return _usecs; }
	off_t Timer::msecs() const { return _usecs/1000; }
	float Timer::msecs_float() const { return _usecs/1000.0; }
	float Timer::secs() const { return _usecs/1000000.0; }
	off_t Timer::usecs_since_mark() const { return _usecs - _usecs_at_mark; }

	/*void convertRailFormatToYoung(string inFilePath, string outFilePath)
	{
		std::ifstream inFile;
		std::ofstream outFile;
		inFile.open(inFilePath.c_str(),std::ios::out);
		outFile.open(outFilePath.c_str(),std::ios::app);
		//if(!file)
		//{
		//	std::cout<<"File nije otvoren!!"<<std::endl;
		//}
		int r,c;
		//matrix_type *A;
		real *w;
		std::string   line;
		int count=0;
		int countNonzeroElements=0;
		int value;
		std::getline(inFile, line);
		//std::cout<<line<<std::endl;
		std::stringstream  linestream(line);
		linestream >> value;
		r=(int)(value);
		linestream >> value;
		c=(int)(value);
		linestream>>value;

		//linestream >> value;

		std::cout<<"r = "<<r<<"; c = "<<c<<std::endl;
		if(r>0&&c>0)
		{
			//A=(matrix_type *)calloc(r*c,sizeof(matrix_type));
			w=(real *)malloc(r*c*sizeof(real));
			outFile<<r<<" "<<c<<std::endl;
		}
		else
		{
			std::cout<<"Number of rows and columns must be positive!"<<std::endl;
			exit(0);
		}
		linestream >> value;
		while(std::getline(inFile, line))
		{

				std::stringstream  linestream(line);
				linestream >> value;
				w[count]=((real)value)/2.0;
				std::cout<<w[count];
				linestream >> value;
				int numOfRows=(int)value;
				int countRows=0;
				std::cout<<" "<<numOfRows<<" ";
				while(linestream >> value)
				{
					int row = (int)value-1;
					std::cout<<row<<" ";
					if(row>=0&&row<r)
					{
						if(w[count]<=0)
						{
							std::cout<<"Negative or zero weight!!! Exit!"<<std::endl;
							exit(0);
						}
						else
						{
							outFile<<count<<" "<<row<<" "<<1.0/w[count]<<std::endl;
							//std::cout<<A[count*r+row]<<" ";
							countNonzeroElements++;
						}

					}
					else
					{
						std::cout<<"Index: Out of the range!!!"<<std::endl;
					}
					countRows++;
				}
				std::cout<<std::endl;
			if(countRows!=numOfRows)
				{
					std::cout<<"Number of rows that are covered by column "<<count+1<<" doesn't match reporter number!!!"<<std::endl;
					exit(0);
				}
				count++;
		}
		if(count!=c)
		{
			std::cout<<"Real number of columns and reported number of columns don't match!!!"<<std::endl;
			exit(0);
		}
		//d=countNonzeroElements/(r*c);
		std::cout<<"Rail Covering Instance created. Number of nonzero elements: "<<countNonzeroElements<<"."<<std::endl;
		inFile.close();
		outFile.close();
	}*/



}
